<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="icon" href="./favicon.ico" type="image/x-icon">
    <title>Molecular Stories</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        #viewer {
            position: absolute;
            left: 0;
            top: 0;
            right: 34%;
            bottom: 0;
        }

        #controls {
            position: absolute;
            left: 66%;
            top: 0;
            right: 0;
            bottom: 0;
            padding: 16px;
            padding-bottom: 20px;
            border: 1px solid #ccc;
            border-left: none;
            background: #F6F5F3;
            z-index: -2;
            display: flex; 
            flex-direction: column;
            gap: 16px;
        }

        #links {
            position: absolute;
            bottom: 4px;
            right: 8px;
            font-family: "Raleway", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif;
            font-size: 0.6rem;
            z-index: -1;
            color: #666;
        }

        #links a {
            color: #666;
            text-decoration: none;
        }

        #links .sep {
            color: #aaa;
        }

        @media (orientation:portrait) {
            #viewer {
                position: absolute;
                left: 0;
                top: 0;
                right: 0;
                bottom: 40%;
            }

            #controls {
                position: absolute;
                left: 0;
                top: 60%;
                right: 0;
                bottom: 0;  
                border-top: none;  
            }

            .msp-viewport-controls-buttons {
                display: none;
            }
        }
    </style>
    <link rel="stylesheet" type="text/css" href="mvs-stories.css" />
    <script type="text/javascript" src="mvs-stories.js"></script>
</head>

<body>
    <!-- the context-name parameter is optional and useful when embedding multiple stories in a single page -->
    <div id="viewer">
        <mvs-stories-viewer context-name="story1" ></mvs-stories-viewer>
    </div>
    <div id="controls">
        <mvs-stories-snapshot-markdown context-name="story1" style="flex-grow: 1;" ></mvs-stories-snapshot-markdown>
    </div>

    <div id="links">
        <span id="open-in-stories" style="display: none;"><a href="#" id="open-in-stories-link" target="_blank" rel="noopener noreferrer" title="Open and edit the story in the MolViewStories app">Edit in MolViewStories</a>&nbsp;<span class="sep">•</span></span>
        <span id="open-in-molstar" style="display: none;"><a href="#" id="open-in-molstar-link" target="_blank" rel="noopener noreferrer" title="Open the story in the Mol* Viewer app. Enables exporting an animation.">Open in Mol* Viewer</a>&nbsp;<span class="sep">•</span></span>
        <a href="#" id="mvs-data" title="MolViewSpec State for this story. Can be opened in the Mol* app.">Download MVS</a> <span class="sep">•</span> <a href="https://github.com/molstar/molstar/tree/master/src/apps/mvs-stories" id="mvs-data" target="_blank" rel="noopener noreferrer">Source Code</a>
    </div>

    <script>
        var urlParams = new URLSearchParams(window.location.search);
        var storyId = urlParams.get('story-id');
        var storyUrl = urlParams.get('story-url');
        var storySessionUrl = urlParams.get('story-session-url');
        var format = urlParams.get('data-format');

        // For testing purposes:
        // if (!storyUrl) {
        //     storyUrl = 'https://raw.githubusercontent.com/molstar/molstar/master/examples/mvs/kinase-story.mvsj';
        // }

        var molstarDataLink = storyUrl;
        var editInStoriesUrl = undefined;

        if (storyId) {
            mvsStories.loadFromID(storyId, { format: format || 'mvsj', contextName: 'story1' });
            editInStoriesUrl = 'https://molstar.org/mol-view-stories/builder?published-session-id=' + storyId;
            molstarDataLink = 'https://stories.molstar.org/api/story/' + storyId + '/data';
        } else if (storyUrl) {
            mvsStories.loadFromURL(storyUrl, { format: format || 'mvsj', contextName: 'story1' });
        }

        if (!editInStoriesUrl && storySessionUrl) {
            editInStoriesUrl = 'https://molstar.org/mol-view-stories/builder?session-url=' + encodeURIComponent(storySessionUrl);
        }
        
        if (molstarDataLink) {
            var molstarLink = 'https://molstar.org/viewer?mvs-url=' + encodeURIComponent(molstarDataLink) + '&mvs-format=' + encodeURIComponent(format || 'mvsj'); 
            document.getElementById('open-in-molstar-link').setAttribute('href', molstarLink);
            document.getElementById('open-in-molstar').style.display = 'inline';
        }
        
        if (editInStoriesUrl) {
            document.getElementById('open-in-stories-link').setAttribute('href', editInStoriesUrl);
            document.getElementById('open-in-stories').style.display = 'inline';
        }

        document.getElementById('mvs-data').addEventListener('click', (e) => {
            e.preventDefault();
            mvsStories.downloadCurrentStory({ contextName: 'story1' });
        });
    </script>
    <!-- __MOLSTAR_ANALYTICS__ -->
</body>

</html>